# -*- coding:utf-8 -*-
# @Time: 2020/9/2 13:50
# @Author: liupengfei
# @File: serializers.py
import datetime

from rest_framework import serializers
from apps.coupons.models import Coupon

from .models import MemberCoupons


class CouponSerializer(serializers.ModelSerializer):
    user = serializers.CharField(source='user.nickname', read_only=True)
    start_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M', read_only=True)
    end_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M', read_only=True)
    status_text = serializers.CharField(source='get_status_display', read_only=True)

    class Meta:
        model = Coupon
        fields = ('id', 'status', 'title', 'money', 'start_time', 'end_time', 'notes', 'user', 'status_text')


class MemberCouponsSerializer(serializers.ModelSerializer):
    coupon = serializers.SerializerMethodField()

    def get_coupon(self, obj):
        return CouponSerializer(instance=obj.coupon).data

    class Meta:
        model = MemberCoupons
        fields = ('id', '')
